/* 
 * Copyright (c) 2019, CENTRIN.CIYUN.LTD. All rights reserved.
 */
package com.cory.admin.sys.controller.portal;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cory.admin.sys.config.AdminConstants;
import com.cory.admin.sys.domain.AdminPrincipal;
import com.cory.admin.sys.service.SysLoginLogService;
import com.cory.boot.component.cache.RedisUtils;

/**
 * 登出
 * @author xiongcong
 * @date 2019-02-26
 */
@Controller
@RequestMapping("portal/logout")
public class LogoutController {
    @Autowired
    private SysLoginLogService sysLoginLogService;

    @GetMapping("")
    public String logout(HttpSession session) {
        AdminPrincipal user = (AdminPrincipal)session.getAttribute(AdminConstants.SESSION_USER_INFO);
        if (user != null) {
            RedisUtils.del(AdminConstants.REDIS_PREFIX_TOKEN + user.getToken());
            session.removeAttribute(AdminConstants.SESSION_USER_INFO);
            session.removeAttribute(AdminConstants.SESSION_USER_RESOURCE);
            
            if (user.getLoginId() != null) {
                sysLoginLogService.addLoginoutLog(user.getLoginId(), "用户退出系统");
                AdminConstants.MAP_LOGIN_TIP_MSG.remove(user.getLoginId());
            }
        }
        
        return "redirect:/login";
    }
    
}
